﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using Gollie.DataTables;
using MySQL.Db;

namespace Gollie.Controls
{
	public partial class DashBoard : AuthenticationRequiredAdministratorBaseUserControl
	{
		public static System.Drawing.Color errorColor = System.Drawing.Color.FromArgb(253, 81, 77);

		private DateTime from;
		private DateTime to;

		protected void Page_Load(object sender, EventArgs e)
		{

		}

		protected override void OnPostBack(object sender, EventArgs e)
		{
			this.GetBalance();

			this.GetDeposits();
		}

		protected override void OnFirstHit(object sender, EventArgs e)
		{
			this.Initialize();

			this.GetBalance();

			this.GetDeposits();
		}

		private void Initialize()
		{
			DateTime toDay = DateTime.Today;
			DateTime firstOfPreviousMonth = toDay.AddMonths(-1).AddDays(-(toDay.Day - 1));
			this.tbDateFrom.Text = ObjectFormatter.GetShortDateString(firstOfPreviousMonth);
			this.tbDateTo.Text = ObjectFormatter.GetShortDateString(DateTime.Today);
		}

		private void GetBalance()
		{
			bool ok = this.ValidatePeriod();

			if (!ok)
				return;

			ResultSet resultSet = Database.Queries.GetVendorCurrentBalance(this.UserInfo.VendorId, from, to.AddDays(1));

			decimal outstanding = resultSet.GetDecimal(0, "outstandingamount");
			decimal totalExpired = resultSet.GetDecimal(0, "expiredamount");
			decimal periodExpired = resultSet.GetDecimal(0, "expiredamountperiod");

			string outstandingBalance = ObjectFormatter.GetCurrencyString(outstanding);
			string expiredBalance = ObjectFormatter.GetCurrencyString(totalExpired);
			string periodExpiredBalance = ObjectFormatter.GetCurrencyString(periodExpired);

			this.lblOutstanding.Text = outstandingBalance;
			this.lblExpired.Text = expiredBalance;
			this.lblPeriodExpired.Text = periodExpiredBalance;
		}

		private void GetDeposits()
		{
			bool ok = this.ValidatePeriod();

			if (!ok)
				return;

			ResultSet resultSet = Database.Queries.GetDepositsPerVendor(this.UserInfo.VendorId, from, to.AddDays(1));

			string deposits = ObjectFormatter.GetCurrencyString(resultSet.GetDecimal(0, "depositamount"));
			string credits = ObjectFormatter.GetCurrencyString(resultSet.GetDecimal(0, "creditamount"));
			string used = ObjectFormatter.GetCurrencyString(resultSet.GetDecimal(0, "usedamount"));

			this.lblDeposit.Text = deposits;
			this.lblGiven.Text = credits;
			this.lblUsed.Text = used;

			this.GetDepositsPerPeriod();
		}

		private bool ValidatePeriod()
		{
			bool ok = true;

			if (DateTime.TryParse(this.tbDateFrom.Text, out from) == false)
			{
				ok = false;
				this.tbDateFrom.BackColor = errorColor;
			}
			else
				this.tbDateFrom.BackColor = System.Drawing.Color.White;

			if (DateTime.TryParse(this.tbDateTo.Text, out to) == false)
			{
				ok = false;
				this.tbDateTo.BackColor = errorColor;
			}
			else
				this.tbDateTo.BackColor = System.Drawing.Color.White;

			if (from > to)
			{
				ok = false;
				this.tbDateFrom.BackColor = errorColor;
				this.tbDateTo.BackColor = errorColor;
			}

			return ok;
		}

		private void GetDepositsPerPeriod()
		{
			DepositsPerVendorPerPeriod deposits = DepositsPerVendorPerPeriod.GetInstance(this.UserInfo.VendorId, from, to.AddDays(1), this.rblPeriod.SelectedValue);

			this.GridView1.DataSource = deposits;

			this.GridView1.DataBind();

			this.GraphControl.FillGraph(deposits);
		}

		public SortDirection SortDir
		{
			get
			{
				if (ViewState["dirState"] == null)
					ViewState["dirState"] = SortDirection.Ascending;

				return (SortDirection)ViewState["dirState"];
			}
			set
			{
				ViewState["dirState"] = value;
			}
		}

		protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
		{
			bool ok = this.ValidatePeriod();

			if (!ok)
				return;

			SortDir = SortDir == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;
			List<DepositsPerVendorPerPeriodItem> list = DepositsPerVendorPerPeriod.GetInstance(this.UserInfo.VendorId, this.from, this.to, this.rblPeriod.SelectedValue).Sort(e.SortExpression, SortDir == SortDirection.Ascending);
			this.GridView1.DataSource = list;
			this.GridView1.DataBind();
		}

		protected void btnExportCSV_Click(object sender, EventArgs e)
		{
			bool ok = this.ValidatePeriod();

			if (!ok)
				return;

			DepositsPerVendorPerPeriod deposits = DepositsPerVendorPerPeriod.GetInstance(this.UserInfo.VendorId, from, to.AddDays(1), this.rblPeriod.SelectedValue);

			using (MemoryStream memStream = new MemoryStream())
			{
				deposits.ExcelExport(this.rblPeriod.SelectedItem.Text, this.UserInfo.VendorId, memStream);

				string fileName = "Gollie.xls";

				Response.ClearContent();
				Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
				Response.ContentType = System.Web.MimeMapping.GetMimeMapping(fileName); //"application/ms-excel";

				memStream.WriteTo(Response.OutputStream);
			}
			Response.End();

		}
	}
}